<?php
include_once("sessionmanager.php");
initSession();

if (!isset($_SESSION['s_loggedin']) || ($_SESSION['s_loggedin'] == false) || !isset($_SESSION['s_serverurl']) || (strpos($_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'],$_SESSION['s_serverurl']) === false)) {
	$error_page_code = "timeout";
	include "errorpage.php";
	exit();
}
if (!$_SESSION['s_caninsert']) {
	$error_page_code = "permission";
	include "errorpage.php";
	exit();
}

include "languages.php";
include_once "functions.inc.php";
include "attributes.php";
$userid = $_SESSION['s_userid'];
$ogameserver = $_SESSION['s_ogameserver'];

makeheader(READ_TITLE);
?>
<div style="padding-left:10px;">
<?php
if (isset($_FILES['userfile']) && !empty($_FILES['userfile']['name'])) {
	// Dateiupload behandeln
	include("javahelp.php");
} else {
	if (isset($_POST['gala']) && !empty($_POST['gala'])) {
		include_once "parser.inc.php";
		include_once "spioparser.inc.php";
		include ("config.php");

		$contents = $_POST['gala'];
		//$contents = utf8_decode($contents);

		if(get_magic_quotes_gpc()) {
			$contents = strip_tags(stripslashes($contents));
		}

		$contents = trim(str_replace("\r","",$contents));

		//combat report
		if($ogameserver == 'ogame.com.cn') {
			$combatreporttitle = "下列的舰队互相对立";
		} else if($ogameserver == 'ogame.org') {
			$combatreporttitle = "the following fleets met in battle";
		} else if($ogameserver == 'ogame.tw') {
			$combatreporttitle = "下列的艦隊互相對立";
		}
		
		if(strpos($contents, $combatreporttitle) !== false) {
			include_once "combatparser.inc.php";
			$cparser = new combatParser($dbusername,$dbpassword,$dbname,$combatreports,$dbhost);
			if ($cparser->parseString($contents) == false) {
				echo $cparser->error."<br>";
			}
		}
		
		//PP rank report
		if($ogameserver == 'ogame.com.cn') {
			$ppreporttitle = "/排名\s+玩家\s+联盟\s+积分/";
		} else if ($ogameserver == 'ogame.org') {
			$ppreporttitle = "/Rank\s+Player\s+Alliance\s+Points/";
		} else if ($ogameserver == 'ogame.tw') {
			$ppreporttitle = "/排名\s+玩家\s+聯盟\s+積分/";
		}
		
		if(preg_match($ppreporttitle, $contents) == 1) {
			include_once "pptrendsparser.inc.php";
			$cparser = new pptrendsParser($dbusername,$dbpassword,$dbname,$pptrends,$pstablename,$dbhost);
			if ($cparser->parseString($contents) == false) {
				echo $cparser->error."<br>";
			}
		}

		if($ogameserver == 'ogame.org'){
			// in Englische Version portieren
			$contents = str_replace("System","Solar system",$contents); // new english
		} else if($ogameserver == 'ogame.com.cn') {
			// for gb2312
			$contents = str_replace("太阳系","Solar system",$contents);
		} else if($ogameserver == 'ogame.tw') {
			// for big5
			$contents = str_replace("太陽系","Solar system",$contents);
		}

		//espionage report
		if($ogameserver == 'ogame.com.cn') {
			$espionagetitle = '/资源在 .* \[\d+:\d+:\d+\] (\([^)]*\)[\r\n]+)?\s*在 \d+-\d+ \d+:\d+:\d+/';
		} else if($ogameserver == 'ogame.org') {
			$espionagetitle = '/Resources on .* \[\d+:\d+:\d+\] (\([^)]*\)[\r\n]+)?\s*at \d+-\d+ \d+:\d+:\d+/';
		} else if($ogameserver == 'ogame.tw') {
			$espionagetitle = '/資源在 .* \[\d+:\d+:\d+\] (\([^)]*\)[\r\n]+)?\s*在 \d+-\d+ \d+:\d+:\d+/';
		}

		if(preg_match($espionagetitle, $contents) == 1) {
			$spioparser = new Probeparser($dbusername,$dbpassword,$dbname,$reporttable,$dbtablename,$dbhost);
			if ($spioparser->parseEntry($contents) == false) {
					echo $spioparser->error."<br>";
			}
		}

		if  (strpos($contents,"Solar system") === false) { 
			// Espionage Reports
			if ($_SESSION['s_probeview'])	{
				if($ogameserver == 'ogame.com.cn' && strncmp($contents, "a:", 2) == 0) {
					$spioparser = new Probeparser($dbusername,$dbpassword,$dbname,$reporttable,$dbtablename,$dbhost);
					if($spioparser->parseEntry2($contents) == false) {
						echo $spioparser->error."<br>";
					}
				}
			} else {
				echo READ_PERMISSION."<br>";
			}
		} else {
			// Galaxy views
			$gala_array = preg_split('/Solar system[\n|\t|\s]+Solar system/',$contents,-1);
			$parser = new Parser($dbusername,$dbpassword,$dbname,$dbtablename,$astablename,$pstablename,$noticetable,$reporttable,$dbhost);
			for ($i=0;$i<count($gala_array);$i++) {
				if(strlen($gala_array[$i]) < 64) continue; //too short
				$queryarray = $parser->parseString("Solar system\n\nSolar system".$gala_array[$i],false,false);
				if ($queryarray === false) {
					echo $parser->error."<br>";
				}
			}
		}
	}
	echo READ_JOB_DONE;
}

?>
</div>
<form name="scans" method="post" action="einlesen.php" enctype="multipart/form-data" accept-charset="utf-8">
<table cellpadding="1" cellspacing="0" border="0" align="center" width="90%"><tr><td>
<table class="standard" cellpadding="4" cellspacing="0" border="0" width="100%">
<tr class="tblhead"><td colspan="2"><?php echo READ_TOPIC; ?></td></tr>
<tr class="firstcolor"><td><?php echo READ_EXAMPLE; ?></td>

<td><a class="link" href="beispiel.php" target="_self">Link</a></td>
</tr>
<tr class='firstcolor' valign='top'><td colspan='2'><?php echo READ_DESCRIPTION; ?></td></tr>
<tr class="firstcolor"><td><?php echo READ_GALAXY; ?></td>
<td><textarea class="textfield" name="gala" cols="65" rows="25"></textarea></td>
</tr>
<tr class="firstcolor" valign="top">

	      	<td align="right"><input class="button" type="submit" name="<?php echo READ_BUTTON; ?>" value="<?php echo READ_BUTTON; ?>" /></td>

	      	<td><input class="button" type="reset" name="Reset" value="Reset" /></td>
</tr></table>
</td></tr>
</table>
</form>
<?php
makefooter();
?>
